package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.videocollege.VideoOrder;
import com.banzhi.shop.model.CallBack.FindVideosCallBack;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/5/14.
 */

public class FindVideosImpl implements IFindVideosModel {

    @Override
    public void getData(String keyword, int page, String type, final FindVideosCallBack callBack) {
        RequestParams params = new RequestParams(URLString.CollegeOrderURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
       // params.addBodyParameter("goodsName",goodsName);
        if (ASUtils.isCanUse(keyword)) {
            params.addBodyParameter("keyword", keyword);
        }
        params.addBodyParameter("page", page + "");
        params.addBodyParameter("type", type);
        LogUtils.d("FindVideosImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        VideoOrder videoOrder = gson.fromJson(result, VideoOrder.class);

                        callBack.ongetDataSuccess( videoOrder.getData().getList());

                    } else {
                        callBack.ongetDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("CollegeOrderURL", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(87)");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    @Override
    public void getData(int page, FindVideosCallBack callBack) {
       getData(null,page,null,callBack);

    }
}
